package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.FileSystem;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Preference;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.Evaluate;
import com.ruoyi.system.service.IEvaluateService;

/**
 * 用户评价Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-04
 */
@Service
public class EvaluateServiceImpl implements IEvaluateService 
{
    @Autowired
    private EvaluateMapper evaluateMapper;

    @Autowired
    private MerchantMapper merchantMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private FileSystemMapper fileSystemMapper;

    @Autowired
    private PreferenceMapper preferenceMapper;
    /**
     * 查询用户评价
     * 
     * @param id 用户评价主键
     * @return 用户评价
     */
    @Override
    public Evaluate selectEvaluateById(Long id)
    {
        return evaluateMapper.selectEvaluateById(id);
    }

    /**
     * 查询用户评价列表
     * 
     * @param evaluate 用户评价
     * @return 用户评价
     */
    @Override
    public List<Evaluate> selectEvaluateList(Evaluate evaluate)
    {
        List<Evaluate> evaluates = evaluateMapper.selectEvaluateList(evaluate);
        evaluates.forEach(evaluateVo -> {
            evaluateVo.setMerchant(merchantMapper.selectMerchantById(evaluateVo.getMerchantId()));
            evaluateVo.setMember(memberMapper.selectMemberById(evaluateVo.getUserId()));
            if (evaluateVo.getPic() != null) {
                evaluateVo.setPicFile(new ArrayList<>());
                String[] split = evaluateVo.getPic().split(",");
                for (int i = 0; i < split.length; i++) {
                    evaluateVo.getPicFile().add(fileSystemMapper.selectFileSystemById(Long.valueOf(split[i])));
                }
            }
        });
        return evaluates;
    }

    /**
     * 新增用户评价
     *
     * @param evaluate 用户评价
     * @return 结果
     */
    @Override
    public int insertEvaluate(Evaluate evaluate)
    {
        if (!evaluate.getPicFile().isEmpty()){
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < evaluate.getPicFile().size(); i++) {
                if (evaluate.getPicFile().get(i)!=null){
                    stringBuilder.append(evaluate.getPicFile().get(i).getId());
                    if (i != evaluate.getPicFile().size() - 1) {
                        stringBuilder.append(",");
                    }
                }
            }
            evaluate.setPic(stringBuilder.toString());
        }
        /**
         * 根据用户ID和商家ID查询用户之前评价内容
         */
        Evaluate evaluate1 = evaluateMapper.selectEvaluateByUserIdAndMerchantId(evaluate.getUserId(), evaluate.getMerchantId());
        if (evaluate1 != null){
            evaluate.setId(evaluate1.getId());
            //计算评分
            BigDecimal add = evaluate.getTaste().add(evaluate.getService()).add(evaluate.getCostEffectiveness()).add(evaluate.getEnvironment());
            evaluate.setTotal(add.divide(BigDecimal.valueOf(4),  2));
            int i = evaluateMapper.updateEvaluate(evaluate);
//            updateMerchantScore(evaluate.getMerchantId());
            return i;
        }
        evaluate.setCreateTime(DateUtils.getNowDate());
        //计算评分
        BigDecimal add = evaluate.getTaste().add(evaluate.getService()).add(evaluate.getCostEffectiveness()).add(evaluate.getEnvironment());
        evaluate.setTotal(add.divide(BigDecimal.valueOf(4),  2));
        int i = evaluateMapper.insertEvaluate(evaluate);
//        updateMerchantScore(evaluate.getMerchantId());
        return i;
    }
    //根据商家店铺ID对商家评分进行更新
    public void updateMerchantScore(Long merchantId) {
       merchantMapper.updateMerchantTaste(merchantId);
       merchantMapper.updateMerchantService(merchantId);
       merchantMapper.updateMerchantCostEffectiveness(merchantId);
       merchantMapper.updateMerchantEnvironment(merchantId);
       merchantMapper.updateMerchantTotal(merchantId);
       merchantMapper.updateMerchantScoreNum(merchantId);
    }

    /**
     * 修改用户评价
     * 
     * @param evaluate 用户评价
     * @return 结果
     */
    @Override
    public int updateEvaluate(Evaluate evaluate)
    {
        return evaluateMapper.updateEvaluate(evaluate);
    }

    /**
     * 批量删除用户评价
     * 
     * @param ids 需要删除的用户评价主键
     * @return 结果
     */
    @Override
    public int deleteEvaluateByIds(Long[] ids)
    {
        return evaluateMapper.deleteEvaluateByIds(ids);
    }

    /**
     * 删除用户评价信息
     * 
     * @param id 用户评价主键
     * @return 结果
     */
    @Override
    public int deleteEvaluateById(Long id)
    {
        return evaluateMapper.deleteEvaluateById(id);
    }

    @Override
    public int insertPreference(Preference preference) {
        preferenceMapper.deletePreference(preference);
        int i = preferenceMapper.insertPreference(preference);
        updateMerchantLike(preference.getMerchantId());
        return i;
    }

    @Override
    public int deletePreferenceByUserId(Preference preference) {
        preferenceMapper.deletePreference(preference);
        updateMerchantLike(preference.getMerchantId());
        return 1;
    }

    @Override
    public int approve(Evaluate evaluate) {
        if (evaluate.getState().equals("1")){
            updateMerchantScore(evaluate.getMerchantId());
        }
        return evaluateMapper.updateEvaluate(evaluate);
    }

    //根据商家ID更新商家的点赞或者踩
    public void updateMerchantLike(Long merchantId) {
        merchantMapper.updateMerchantLike(merchantId);
    }
}
